import 'package:flutter/material.dart';

/// iOS风格的页面切换动画
class CupertinoStyleRoute<T> extends PageRouteBuilder<T> {
  final Widget page;
  
  CupertinoStyleRoute({required this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) {
            const begin = Offset(1.0, 0.0);
            const end = Offset.zero;
            const curve = Curves.easeInOut;
            
            // 创建滑动动画
            var tween = Tween(begin: begin, end: end)
                .chain(CurveTween(curve: curve));
            var offsetAnimation = animation.drive(tween);

            // 创建渐变动画
            var fadeAnimation = animation.drive(
              Tween(begin: 0.0, end: 1.0)
                  .chain(CurveTween(curve: curve)),
            );

            return SlideTransition(
              position: offsetAnimation,
              child: FadeTransition(
                opacity: fadeAnimation,
                child: child,
              ),
            );
          },
          // 设置转场持续时间
          transitionDuration: const Duration(milliseconds: 350),
          // 设置返回转场持续时间
          reverseTransitionDuration: const Duration(milliseconds: 350),
        );
}

/// 路由管理器
class AppRouter {
  /// 导航到新页面
  static Future<T?> push<T>(BuildContext context, Widget page) {
    return Navigator.push<T>(
      context,
      CupertinoStyleRoute(page: page),
    );
  }

  /// 替换当前页面
  static Future<T?> pushReplacement<T>(BuildContext context, Widget page) {
    return Navigator.pushReplacement(
      context,
      CupertinoStyleRoute(page: page),
    );
  }

  /// 移除所有页面并导航到新页面
  static Future<T?> pushAndRemoveUntil<T>(BuildContext context, Widget page) {
    return Navigator.pushAndRemoveUntil(
      context,
      CupertinoStyleRoute(page: page),
      (route) => false,
    );
  }
} 